{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "attempted-transaction",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decreased-snake",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "stable-courtesy",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "scenic-binding",
   "metadata": {
    "code_folding": [],
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建将列表转化成numpy数组\n",
    "l = [1,2,3,4,5]\n",
    "\n",
    "nd1 = np.array(l)\n",
    "print(nd1)\n",
    "display(nd1) # 显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "enormous-drill",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape = (3,4), dtype = np.int16)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "varying-estate",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape = (3,5), dtype = np.float32)\n",
    "nd3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "spiritual-statement",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#3维数组\n",
    "nd4 = np.full(shape = (3,4,5), fill_value=3.1415926)# 生成任意指定的数组\n",
    "nd4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "therapeutic-concentration",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([71, 58, 69, 12, 88, 43, 36, 29, 36, 22, 79, 48, 88, 79, 14, 26, 16,\n",
       "       34, 51,  9])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0,100, size = 20)#从0到100 生成随机数字int，\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "aggregate-equality",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.16822305, 0.76676662, 0.33513152, 0.63250902, 0.53042946],\n",
       "       [0.63143921, 0.6558893 , 0.23079849, 0.6817116 , 0.06542293],\n",
       "       [0.77595761, 0.13115732, 0.79585199, 0.13489375, 0.97647304]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 = np.random.rand(3, 5)#生成0-1之间的随机数\n",
    "nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "boolean-secretary",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.52077626,  0.70175585,  0.03132113,  0.30147088,  0.2932221 ],\n",
       "       [ 1.05048081, -1.06204365, -0.32537025, -0.85886405,  0.34558827],\n",
       "       [ 1.15315201,  0.84116436,  0.66711943,  0.44426478,  0.14321635]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd7 = np.random.randn(3,5)# 正态分布，平均值为0，标准差是1\n",
    "nd7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "legislative-destiny",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[175.17991209, 185.48036289, 187.38607495, 169.25207222,\n",
       "        185.62412923],\n",
       "       [161.98312671, 180.77262797, 174.09015296, 167.82312911,\n",
       "        173.52776989],\n",
       "       [177.29657329, 174.65459431, 148.27443499, 181.75990761,\n",
       "        167.87653215]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd8 = np.random.normal(loc = 175, scale = 10, size = (3,5))# 正态分布，评平均值175，标准差是10\n",
    "nd8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "opposed-institute",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n",
       "       35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,\n",
       "       69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd9 = np.arange(1,100, step = 2) #等差数列，左闭右开，取不到100\n",
    "nd9\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "still-tokyo",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  5.5, 11. , 16.5, 22. , 27.5, 33. , 38.5, 44. , 49.5, 55. ,\n",
       "       60.5, 66. , 71.5, 77. , 82.5, 88. , 93.5, 99. ])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd10 = np.linspace(0, 99, num = 19) #等差数列，左闭右闭，num表示数列长度\n",
    "nd10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "computational-relay",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "grateful-easter",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.47608946, -0.75336223, -0.99860021],\n",
       "       [ 0.9427737 ,  1.8140662 ,  0.68918398],\n",
       "       [-0.17139325,  1.40363182,  1.3808586 ],\n",
       "       [ 1.18612022,  1.51206693,  0.2813392 ],\n",
       "       [ 0.59438556, -0.42319201, -0.21492748]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "nd = np.random.randn(5,3)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "surprised-james",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组形状,返回了形状shape\n",
    "nd.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fossil-sandwich",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组数据类型，float 64位，一位占一个0或1\n",
    "nd.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "advisory-count",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组尺寸，数组可以是多维的，请问里面共有多少个数据\n",
    "nd.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fifteen-cooperative",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组维度\n",
    "nd.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "advanced-denial",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "located-slovenia",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 条目 尺寸长度 8字节\n",
    "nd.itemsize\n",
    "# 数据类型为float64 64位-->1个字节8位--->64/8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "parliamentary-duplicate",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "decent-bailey",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[98, 87, 19, 18,  4],\n",
       "       [76, 87, 28, 36, 40],\n",
       "       [20, 57, 75, 43, 67]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.10268153,  0.64225484,  0.86097172,  0.16911675,  0.26648433],\n",
       "       [ 0.64674885, -0.89943731, -1.08416316,  0.38195894, -1.77793147],\n",
       "       [-0.31813271,  0.28523355, -0.53491995, -0.09445376,  0.42762121]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 100, size = (3,5))\n",
    "nd2 = np.random.randn(3,5)\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "equipped-network",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.save('./data', nd1) # 把一个数据存到文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "sweet-receipt",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[98, 87, 19, 18,  4],\n",
       "       [76, 87, 28, 36, 40],\n",
       "       [20, 57, 75, 43, 67]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data.npy')# 默认添加npy后缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "confused-senior",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 多个数据存到一个文件中\n",
    "np.savez('./data.npz', a = nd1, abc = nd2)# 保存数据时起的名字：a, abc称为key，自己命名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "likely-tenant",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "data = np.load('./data.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "extended-flavor",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[98, 87, 19, 18,  4],\n",
       "        [76, 87, 28, 36, 40],\n",
       "        [20, 57, 75, 43, 67]]),\n",
       " array([[-0.10268153,  0.64225484,  0.86097172,  0.16911675,  0.26648433],\n",
       "        [ 0.64674885, -0.89943731, -1.08416316,  0.38195894, -1.77793147],\n",
       "        [-0.31813271,  0.28523355, -0.53491995, -0.09445376,  0.42762121]]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数据\n",
    "['a'], data['abc']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "entertaining-thomas",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.savez_compressed(\"./data2.npz\", x = nd1, y = nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "associate-mention",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[98, 87, 19, 18,  4],\n",
       "       [76, 87, 28, 36, 40],\n",
       "       [20, 57, 75, 43, 67]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load(\"./data2.npz\")['x']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "blind-combination",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 文件名，数据，格式，分隔符\n",
    "np.savetxt(\"./data.txt\", X = nd1, fmt = '%0.2f', delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "partial-resistance",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 文件名，数据，格式，分隔符\n",
    "np.savetxt(\"./data.csv\", X = nd1, fmt = '%d', delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "built-ranking",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[98., 87., 19., 18.,  4.],\n",
       "       [76., 87., 28., 36., 40.],\n",
       "       [20., 57., 75., 43., 67.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt(\"./data.csv\", delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "contemporary-madness",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "temporal-morgan",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 7], dtype=int8)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# int8, int16, int32, int64, uint8无符号\n",
    "# float 16, float32, float64\n",
    "# str\n",
    "# int8 表示2**8个数字 256个 -128~127 有符号\n",
    "# uint8 0~255\n",
    "\n",
    "np.array([2,4,7], dtype = np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "known-negotiation",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([253, 249, 255, 108,   0,   2], dtype=uint8)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([-3,-7,255,108,0, 258], dtype = np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "variable-correspondence",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([86, 81, 14, 84,  5, 97, 34,  0, 71, 51])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0.,100, size=10, dtype = 'int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "robust-italy",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.50782001, 0.12344881],\n",
       "       [0.38764766, 0.39901844],\n",
       "       [0.21347339, 0.18866353],\n",
       "       [0.66118199, 0.90607274],\n",
       "       [0.70242408, 0.63287578],\n",
       "       [0.44445647, 0.32467123],\n",
       "       [0.9595393 , 0.73145097],\n",
       "       [0.29427543, 0.21158913],\n",
       "       [0.93439703, 0.84516574],\n",
       "       [0.99023645, 0.88880743]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd = np.random.rand(10,2)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "corresponding-courage",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "understanding-sister",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.45949018, 0.35946393],\n",
       "       [0.14931655, 0.06047814],\n",
       "       [0.61778134, 0.47289976],\n",
       "       [0.07900067, 0.7524564 ],\n",
       "       [0.28175357, 0.1456229 ],\n",
       "       [0.8193847 , 0.06964733],\n",
       "       [0.28298685, 0.6096643 ],\n",
       "       [0.7122371 , 0.15161525],\n",
       "       [0.27905822, 0.2172145 ],\n",
       "       [0.5619097 , 0.8326834 ]], dtype=float32)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(nd, dtype = 'float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "willing-period",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.508 , 0.1235],\n",
       "       [0.3877, 0.399 ],\n",
       "       [0.2135, 0.1887],\n",
       "       [0.661 , 0.9062],\n",
       "       [0.7026, 0.633 ],\n",
       "       [0.4443, 0.3247],\n",
       "       [0.9595, 0.7314],\n",
       "       [0.2942, 0.2115],\n",
       "       [0.9346, 0.845 ],\n",
       "       [0.99  , 0.8887]], dtype=float16)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.astype(dtype = np.float16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "medium-shelf",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "initial-organizer",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "antique-machinery",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 4, 7, 1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 9, 9, 4, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 加减乘除指数幂运算\n",
    "nd1 = np.random.randint(0,10,size=5)\n",
    "nd2 = np.random.randint(0,10,size=5)\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "sixth-benjamin",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3, -3, -5,  3, -5])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 - nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "multiple-terminal",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 54, 36, 28,  6])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "explicit-china",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-9-666f68945af8>:1: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  nd1 /nd2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([       inf, 0.66666667, 0.44444444, 1.75      , 0.16666667])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 /nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "induced-issue",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([       1, 10077696,   262144,     2401,        1], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1**nd2 # 幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "herbal-dynamics",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(2,3) # 表示2的3次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "governmental-wagon",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([       1, 10077696,   262144,     2401,        1], dtype=int32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "variable-waste",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.605170185988092"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(100) # 底数为自然底数e 2.718"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "allied-chemistry",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log10(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "auburn-psychology",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log2(1024) # 返回10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "worse-pleasure",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "planned-buddy",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 4, 7, 1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 9, 9, 4, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "pretty-boundary",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1>nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "billion-married",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 >= nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "surface-river",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 == nd2 # 两个等号表示是否相等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "featured-coach",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组与标量计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "latter-evans",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13, 16, 14, 17, 11])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字3，4，5...都是标量\n",
    "nd1 + 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "excessive-cricket",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 4, 7, 1])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "special-fluid",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1021, -1018, -1020, -1017, -1023])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 - 1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "difficult-asthma",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 768, 1536, 1024, 1792,  256])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1*256"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "meaning-indian",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.33333333, 0.16666667, 0.25      , 0.14285714, 1.        ])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组可以作为分母,注意不能为0\n",
    "1/nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deadly-copying",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### -= += *=直接改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "compressed-partner",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 4, 7, 1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 9, 9, 4, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2) # 之前的运算不改变原数组，而是产生一个新的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "proper-vocabulary",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "nd1 -= 100 # 没有打印输出，说明改变了原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "scientific-hampton",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-97, -94, -96, -93, -99])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "alike-mixture",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100, 109, 109, 104, 106])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 += 100\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "junior-morocco",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-291, -282, -288, -279, -297])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1*=3\n",
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "anticipated-sacramento",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "No loop matching the specified signature and casting was found for ufunc true_divide",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-31-5d480f654999>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnd1\u001b[0m \u001b[1;33m/=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnd1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: No loop matching the specified signature and casting was found for ufunc true_divide"
     ]
    }
   ],
   "source": [
    "nd1 /= 10 #数组不支持/=\n",
    "nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "paperback-victoria",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 复制和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exposed-majority",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 完全没有复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "agreed-penguin",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 3, 1, 6, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 3, 1, 6, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = 5)\n",
    "b=a\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "marine-championship",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "extensive-chrome",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024,    3,    1,    6,    6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024,    3,    1,    6,    6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024 # 改变a，b也会变\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "global-mining",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 视图、查看、或浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "acoustic-reconstruction",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([41, 30, 54, 31,  5]), array([41, 30, 54, 31,  5]))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 5)\n",
    "b = a.view()# 浅拷贝\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "portable-columbia",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "decent-batman",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.owndata # a数组的数据是自己的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "effective-dutch",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flags.owndata # b是浅拷贝的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "hidden-frontier",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024, 2048,   54,   31,    5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024, 2048,   54,   31,    5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048 # 无论修改谁，两个数组都发生了变化\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vital-bible",
   "metadata": {
    "hidden": true
   },
   "source": [
    "###  深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "functioning-opening",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-15, -34,  -3, -29, -17, -60, -51, -37, -31, -17])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-15, -34,  -3, -29, -17, -60, -51, -37, -31, -17])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(-100,0,size = 10)\n",
    "b = a.copy()\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "mobile-sponsorship",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(a is b)\n",
    "display(a.flags.owndata)\n",
    "display(b.flags.owndata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "beneficial-wiring",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024,  -34,   -3,  -29,  -17,  -60,  -51,  -37,  -31,  -17])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ -15,  -34, 2048,  -29,  -17,  -60,  -51,  -37,  -31,  -17])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[2] = 2048 # 井水不犯河水\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "classified-croatia",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n",
       "       9.9999998e+07, 9.9999999e+07])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1e8) # 0-1亿，数据量非常多\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "infinite-grenada",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "b = a[[1,3,5,7,9,99]].copy() # 去除一部分数据，原来的数组没有了，但是占内存特别大。\n",
    "del a # 不在需要a，删除，释放内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "likely-bonus",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  3.,  5.,  7.,  9., 99.])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "straight-chemistry",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 索引、切片、迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "refined-promotion",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 基本索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "multiple-marker",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 17, 28,  1,  4, 25, 12, 29,  6,  0])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,30,size = 10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "bronze-editor",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17,  1, 25])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1]  #1个\n",
    "a[[1,3,5]] # 取多个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "beneficial-navigation",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 17, 28])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:3] # 左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "stuffed-scoop",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 17, 28])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:3] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "younger-football",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([25, 12, 29,  6])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "worthy-lightning",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([25, 12, 29,  6,  0])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "brief-implement",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 28,  4, 12,  6])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2] # 每两个取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "regulated-landscape",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 12,  0])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3::3] # 从索引3开始，每3个取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "spread-machinery",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  6, 29, 12, 25,  4,  1, 28, 17,  2])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]  # 倒叙"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "resident-disclaimer",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 17, 28,  1,  4, 25, 12, 29,  6,  0])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "naked-retro",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([25, 28])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5::-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "embedded-vietnamese",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17,  1, 25])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:7:2] # 从索引1 开始到7 结束，每两个取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "french-optics",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  9,  8,  3, 18,  3, 11, 15,  5, 13],\n",
       "       [ 6, 10, 17, 17,  5,  9, 23, 11, 29,  7],\n",
       "       [ 0, 15,  2, 29,  7, 24,  9, 16, 27, 18],\n",
       "       [28,  9,  5, 25,  5, 29, 18,  6, 28, 18],\n",
       "       [ 4,  7, 14,  1, 14,  0,  5,  8, 13,  9],\n",
       "       [12, 15, 25, 11, 16,  8, 19, 20, 14, 12],\n",
       "       [ 3,  4, 21, 14,  8,  5, 10,  0, 14, 10],\n",
       "       [ 8,  0, 11, 12,  3,  0,  3, 15, 16, 11],\n",
       "       [13,  6,  2, 16, 22, 13,  2, 25, 14, 27],\n",
       "       [16, 14, 19, 23,  5, 12, 13, 29,  6,  1]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,30,size = (10,10))\n",
    "b  # 二维数组，切片规律一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "recovered-cleaners",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 10, 17, 17,  5,  9, 23, 11, 29,  7])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "close-worker",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  9,  8,  3, 18,  3, 11, 15,  5, 13],\n",
       "       [28,  9,  5, 25,  5, 29, 18,  6, 28, 18],\n",
       "       [12, 15, 25, 11, 16,  8, 19, 20, 14, 12]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[[0,3,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "italic-colleague",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1,1] #取一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "fitted-harbor",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 29, 18])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[3,[2,5,6]]  # 多为数组，用逗号分隔开来就行了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "illegal-geneva",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15,  7, 16],\n",
       "       [14,  5, 29]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2::7,1::3] # 行：索引2和7。列从1开始，每3个中取一个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "backed-elements",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-1,-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "owned-excuse",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-2,-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "logical-ranking",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([14, 25,  2])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-2,[-2,-3,-4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "loose-roots",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 花式索引,索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "speaking-embassy",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]),\n",
       " array([   2,    4,    4,    6,    8,    8, 1024]))"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([1,2,3,4,5,6,7,8,9,10])\n",
    "arr2 = arr1[[1,3,3,5,7,7,7]] # 输出 array([2, 4, 4, 6, 8, 8, 8])\n",
    "arr2[-1] = 1024 # 修改值，不影响arr1\n",
    "arr1,arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "settled-rachel",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 87, 120, 128],\n",
       "       [129, 148,  24],\n",
       "       [107, 126,  79],\n",
       "       [ 24,  21, 148],\n",
       "       [ 71, 135,   0],\n",
       "       [ 91,  44,  27],\n",
       "       [ 25, 112,  28],\n",
       "       [142, 137,  44],\n",
       "       [ 37, 124,  55],\n",
       "       [  6,  98,  59],\n",
       "       [142,  70,   1],\n",
       "       [ 78, 134, 136],\n",
       "       [138,  32, 128],\n",
       "       [101, 121,  78],\n",
       "       [ 81,  47, 130],\n",
       "       [ 15,  45,  99],\n",
       "       [127, 106, 146],\n",
       "       [124,  49,  51],\n",
       "       [102,   5, 100],\n",
       "       [ 44,  58, 119],\n",
       "       [ 76, 126, 127],\n",
       "       [  3,   6,  20],\n",
       "       [147, 109,   0],\n",
       "       [ 55,  72,  83],\n",
       "       [ 90, 116,  45],\n",
       "       [ 24,  74,  41],\n",
       "       [ 42,  37,  36],\n",
       "       [ 10,  41, 134],\n",
       "       [ 55, 131, 143],\n",
       "       [ 81,  21,  88],\n",
       "       [105,  43, 108],\n",
       "       [ 97,  68,  42],\n",
       "       [ 59,  81,  87],\n",
       "       [ 94,  57, 119],\n",
       "       [ 64,  57,  85],\n",
       "       [132,   9, 143],\n",
       "       [ 42,  69,  14],\n",
       "       [ 34,  15, 139],\n",
       "       [113, 119,  58],\n",
       "       [ 60,  98, 150],\n",
       "       [ 70, 122, 117],\n",
       "       [  3,  42,  59],\n",
       "       [144,  26, 131],\n",
       "       [119,  38,  93],\n",
       "       [109,  35,   8],\n",
       "       [129, 120, 142],\n",
       "       [ 53,  93,  46],\n",
       "       [ 11, 128, 124],\n",
       "       [124,  36,  23],\n",
       "       [ 24,   2,  15],\n",
       "       [ 83,  28,   6],\n",
       "       [103, 127, 142],\n",
       "       [ 40, 147, 108],\n",
       "       [ 81,  15, 130],\n",
       "       [115,  70,  71],\n",
       "       [  8,  35,  97],\n",
       "       [ 56,  75, 144],\n",
       "       [ 26,   0,  88],\n",
       "       [122,  71,  15],\n",
       "       [147,  36, 141],\n",
       "       [ 97,  53,  92],\n",
       "       [149, 135, 149],\n",
       "       [  5,  57,  80],\n",
       "       [ 41,  32,   5],\n",
       "       [126,  96,   2],\n",
       "       [ 67, 125,  14],\n",
       "       [ 42,  24,  71],\n",
       "       [147, 134,  47],\n",
       "       [145,  42,  95],\n",
       "       [129,  97,  85],\n",
       "       [ 71,   7,  27],\n",
       "       [143,  23,  21],\n",
       "       [ 75,  82,  96],\n",
       "       [ 32, 119,  97],\n",
       "       [ 43, 140,  72],\n",
       "       [ 19,  38,  93],\n",
       "       [ 52, 142,  70],\n",
       "       [ 21,  78,  78],\n",
       "       [142,  65,   3],\n",
       "       [ 26,  91,  52],\n",
       "       [ 14,  66, 104],\n",
       "       [143,  23,  58],\n",
       "       [ 49, 116,  26],\n",
       "       [118, 105, 100],\n",
       "       [ 48,  50,  49],\n",
       "       [ 17, 104,  59],\n",
       "       [111,  40,  58],\n",
       "       [ 94,  60, 100],\n",
       "       [ 70,  83,  91],\n",
       "       [146,  69, 104],\n",
       "       [  1, 133,  74],\n",
       "       [122, 124, 141],\n",
       "       [ 87,   7,  35],\n",
       "       [ 34, 117,   9],\n",
       "       [ 68,  91,  41],\n",
       "       [ 11,  86,   5],\n",
       "       [ 85,   7,   1],\n",
       "       [130,  74, 130],\n",
       "       [ 79,  35,   4],\n",
       "       [113,  85,  17]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,151,size=(100,3))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "infrared-robertson",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([120, 128, 129, 148, 126, 148, 135, 142, 137, 124, 142, 134, 136,\n",
       "       138, 128, 121, 130, 127, 146, 124, 126, 127, 147, 134, 131, 143,\n",
       "       132, 143, 139, 150, 122, 144, 131, 129, 120, 142, 128, 124, 124,\n",
       "       127, 142, 147, 130, 144, 122, 147, 141, 149, 135, 149, 126, 125,\n",
       "       147, 134, 145, 129, 143, 140, 142, 142, 143, 146, 133, 122, 124,\n",
       "       141, 130, 130])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = a>=120 # 大于120的条件\n",
    "a[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "dense-andrews",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[129, 120, 142],\n",
       "       [149, 135, 149],\n",
       "       [122, 124, 141]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# bool值 True = 1；False = 0\n",
    "# 三门科目的条件相乘，找出同时大于120 的学生\n",
    "cond2 = cond[:,0]*cond[:,1]*cond[:,2]\n",
    "a[cond2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "expensive-diana",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  6, 20],\n",
       "       [24,  2, 15]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大于等于120，小于等于30\n",
    "cond1 = a >= 120\n",
    "cond2 = a <= 30\n",
    "a[cond2[:,0]*cond2[:,-1]*cond2[:,-2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "assisted-discipline",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "breeding-identification",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数组变形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "consecutive-setup",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 8, 7, 3],\n",
       "       [7, 2, 4, 8, 5],\n",
       "       [5, 6, 8, 9, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randint(0,10,size = (3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dimensional-chamber",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 8],\n",
       "       [7, 3, 7],\n",
       "       [2, 4, 8],\n",
       "       [5, 5, 6],\n",
       "       [8, 9, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5,3) # 只是改变形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "sweet-bumper",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[7]],\n",
       "\n",
       "       [[3]],\n",
       "\n",
       "       [[7]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[4]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[5]],\n",
       "\n",
       "       [[5]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[9]],\n",
       "\n",
       "       [[6]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15,1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "developed-lecture",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 8],\n",
       "       [7, 3, 7],\n",
       "       [2, 4, 8],\n",
       "       [5, 5, 6],\n",
       "       [8, 9, 6]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,3) # -1表示数据，3自动计算-1 = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brief-light",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "orange-range",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 7, 5],\n",
       "       [6, 2, 6],\n",
       "       [8, 4, 8],\n",
       "       [7, 8, 9],\n",
       "       [3, 5, 6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T # 矩阵转置 shape = (5,3),T为transpose方法的缩写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "advisory-chamber",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 7, 5],\n",
       "       [6, 2, 6],\n",
       "       [8, 4, 8],\n",
       "       [7, 8, 9],\n",
       "       [3, 5, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a,(1,0)) # 行0，列1。默认情况下（0，1）-->调整为（1，0）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "indoor-failure",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[7, 6, 4, 4, 0, 5, 2],\n",
       "        [7, 4, 9, 1, 1, 6, 8],\n",
       "        [0, 8, 6, 5, 9, 5, 9],\n",
       "        [3, 7, 9, 9, 2, 7, 3],\n",
       "        [8, 1, 0, 9, 6, 6, 8]],\n",
       "\n",
       "       [[6, 1, 9, 0, 3, 5, 6],\n",
       "        [4, 1, 1, 7, 6, 0, 5],\n",
       "        [2, 1, 0, 1, 0, 3, 6],\n",
       "        [2, 7, 8, 5, 5, 4, 4],\n",
       "        [9, 2, 7, 8, 9, 7, 7]],\n",
       "\n",
       "       [[3, 9, 5, 8, 1, 5, 9],\n",
       "        [3, 7, 5, 8, 9, 8, 1],\n",
       "        [6, 6, 0, 9, 3, 4, 2],\n",
       "        [6, 0, 1, 1, 0, 0, 5],\n",
       "        [0, 7, 6, 4, 7, 9, 8]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,10,size = (3,5,7))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "solved-parks",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[7, 6, 3],\n",
       "        [7, 4, 3],\n",
       "        [0, 2, 6],\n",
       "        [3, 2, 6],\n",
       "        [8, 9, 0]],\n",
       "\n",
       "       [[6, 1, 9],\n",
       "        [4, 1, 7],\n",
       "        [8, 1, 6],\n",
       "        [7, 7, 0],\n",
       "        [1, 2, 7]],\n",
       "\n",
       "       [[4, 9, 5],\n",
       "        [9, 1, 5],\n",
       "        [6, 0, 0],\n",
       "        [9, 8, 1],\n",
       "        [0, 7, 6]],\n",
       "\n",
       "       [[4, 0, 8],\n",
       "        [1, 7, 8],\n",
       "        [5, 1, 9],\n",
       "        [9, 5, 1],\n",
       "        [9, 8, 4]],\n",
       "\n",
       "       [[0, 3, 1],\n",
       "        [1, 6, 9],\n",
       "        [9, 0, 3],\n",
       "        [2, 5, 0],\n",
       "        [6, 9, 7]],\n",
       "\n",
       "       [[5, 5, 5],\n",
       "        [6, 0, 8],\n",
       "        [5, 3, 4],\n",
       "        [7, 4, 0],\n",
       "        [6, 7, 9]],\n",
       "\n",
       "       [[2, 6, 9],\n",
       "        [8, 5, 1],\n",
       "        [9, 6, 2],\n",
       "        [3, 4, 5],\n",
       "        [8, 7, 8]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b,(2,1,0)) # 调整维度结构\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "residential-planning",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数据堆叠合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "laden-depression",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 4, 4],\n",
       "       [0, 8, 6, 5, 9],\n",
       "       [4, 2, 0, 3, 1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[3, 6, 9, 3, 6],\n",
       "       [3, 1, 8, 1, 4],\n",
       "       [9, 8, 2, 8, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,10,size = (3,5))\n",
    "nd2 = np.random.randint(0,10,size = (3,5))\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ordered-livestock",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 4, 4],\n",
       "       [0, 8, 6, 5, 9],\n",
       "       [4, 2, 0, 3, 1],\n",
       "       [3, 6, 9, 3, 6],\n",
       "       [3, 1, 8, 1, 4],\n",
       "       [9, 8, 2, 8, 4]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1,nd2]) # 默认合并为行增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fifth-telescope",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 4, 4, 3, 6, 9, 3, 6],\n",
       "       [0, 8, 6, 5, 9, 3, 1, 8, 1, 4],\n",
       "       [4, 2, 0, 3, 1, 9, 8, 2, 8, 4]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改axis参数，调整数据合并方向\n",
    "np.concatenate([nd1,nd2], axis = 1) # axis 轴，方向，0=行，1=列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "split-eating",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 4, 4, 3, 6, 9, 3, 6],\n",
       "       [0, 8, 6, 5, 9, 3, 1, 8, 1, 4],\n",
       "       [4, 2, 0, 3, 1, 9, 8, 2, 8, 4]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((nd1,nd2)) # 堆叠，增多，合并,h表示水平（列增大）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "respiratory-secretariat",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 4, 4],\n",
       "       [0, 8, 6, 5, 9],\n",
       "       [4, 2, 0, 3, 1],\n",
       "       [3, 6, 9, 3, 6],\n",
       "       [3, 1, 8, 1, 4],\n",
       "       [9, 8, 2, 8, 4],\n",
       "       [3, 6, 9, 3, 6],\n",
       "       [3, 1, 8, 1, 4],\n",
       "       [9, 8, 2, 8, 4],\n",
       "       [4, 2, 1, 4, 4],\n",
       "       [0, 8, 6, 5, 9],\n",
       "       [4, 2, 0, 3, 1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((nd1,nd2,nd2,nd1)) # v表示竖直，行增加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "increased-revolution",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组的拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "recent-lounge",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[83, 57, 56, 17, 21, 42, 35, 59, 96, 75],\n",
       "       [62,  2, 14, 80, 15, 57, 80, 24, 50, 59],\n",
       "       [54, 13, 53, 48, 11, 12, 54, 54, 47, 47],\n",
       "       [83, 63, 73, 26, 77,  4, 15, 92,  3, 46],\n",
       "       [83, 80, 78, 93, 54, 43, 75, 57,  9, 86],\n",
       "       [22, 69, 54,  0, 58, 59, 81, 96, 62, 73],\n",
       "       [62,  6,  8, 31,  8, 31,  4, 86, 99, 14],\n",
       "       [99, 43, 25, 40, 64, 50, 18, 87, 42, 87],\n",
       "       [34, 55, 82, 41, 45, 87, 11, 21, 33,  9],\n",
       "       [82, 44, 62, 23, 81, 51,  3, 56, 78, 12],\n",
       "       [66, 73, 21, 51, 51, 53,  1, 51,  1, 20],\n",
       "       [38, 42, 18, 83, 39, 11, 88,  7, 94, 12],\n",
       "       [57, 65, 39, 66, 58, 10, 48, 31, 93, 74],\n",
       "       [81, 57, 13, 27, 42, 82,  5, 22, 45,  0],\n",
       "       [73, 81, 85, 70, 31,  0,  8, 85, 59, 10]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = (15,10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "handed-marijuana",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 57, 56, 17, 21, 42, 35, 59, 96, 75],\n",
       "        [62,  2, 14, 80, 15, 57, 80, 24, 50, 59],\n",
       "        [54, 13, 53, 48, 11, 12, 54, 54, 47, 47],\n",
       "        [83, 63, 73, 26, 77,  4, 15, 92,  3, 46],\n",
       "        [83, 80, 78, 93, 54, 43, 75, 57,  9, 86]]),\n",
       " array([[22, 69, 54,  0, 58, 59, 81, 96, 62, 73],\n",
       "        [62,  6,  8, 31,  8, 31,  4, 86, 99, 14],\n",
       "        [99, 43, 25, 40, 64, 50, 18, 87, 42, 87],\n",
       "        [34, 55, 82, 41, 45, 87, 11, 21, 33,  9],\n",
       "        [82, 44, 62, 23, 81, 51,  3, 56, 78, 12]]),\n",
       " array([[66, 73, 21, 51, 51, 53,  1, 51,  1, 20],\n",
       "        [38, 42, 18, 83, 39, 11, 88,  7, 94, 12],\n",
       "        [57, 65, 39, 66, 58, 10, 48, 31, 93, 74],\n",
       "        [81, 57, 13, 27, 42, 82,  5, 22, 45,  0],\n",
       "        [73, 81, 85, 70, 31,  0,  8, 85, 59, 10]])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=3) # 数字（要能除尽），表示平均分成多少份，默认为行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "physical-breach",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 57, 56, 17, 21],\n",
       "        [62,  2, 14, 80, 15],\n",
       "        [54, 13, 53, 48, 11],\n",
       "        [83, 63, 73, 26, 77],\n",
       "        [83, 80, 78, 93, 54],\n",
       "        [22, 69, 54,  0, 58],\n",
       "        [62,  6,  8, 31,  8],\n",
       "        [99, 43, 25, 40, 64],\n",
       "        [34, 55, 82, 41, 45],\n",
       "        [82, 44, 62, 23, 81],\n",
       "        [66, 73, 21, 51, 51],\n",
       "        [38, 42, 18, 83, 39],\n",
       "        [57, 65, 39, 66, 58],\n",
       "        [81, 57, 13, 27, 42],\n",
       "        [73, 81, 85, 70, 31]]),\n",
       " array([[42, 35, 59, 96, 75],\n",
       "        [57, 80, 24, 50, 59],\n",
       "        [12, 54, 54, 47, 47],\n",
       "        [ 4, 15, 92,  3, 46],\n",
       "        [43, 75, 57,  9, 86],\n",
       "        [59, 81, 96, 62, 73],\n",
       "        [31,  4, 86, 99, 14],\n",
       "        [50, 18, 87, 42, 87],\n",
       "        [87, 11, 21, 33,  9],\n",
       "        [51,  3, 56, 78, 12],\n",
       "        [53,  1, 51,  1, 20],\n",
       "        [11, 88,  7, 94, 12],\n",
       "        [10, 48, 31, 93, 74],\n",
       "        [82,  5, 22, 45,  0],\n",
       "        [ 0,  8, 85, 59, 10]])]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=2,axis=1) # axis=1表示列被平均分成两份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "announced-israel",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 57, 56, 17, 21, 42, 35, 59, 96, 75]]),\n",
       " array([[62,  2, 14, 80, 15, 57, 80, 24, 50, 59],\n",
       "        [54, 13, 53, 48, 11, 12, 54, 54, 47, 47],\n",
       "        [83, 63, 73, 26, 77,  4, 15, 92,  3, 46],\n",
       "        [83, 80, 78, 93, 54, 43, 75, 57,  9, 86]]),\n",
       " array([[22, 69, 54,  0, 58, 59, 81, 96, 62, 73],\n",
       "        [62,  6,  8, 31,  8, 31,  4, 86, 99, 14],\n",
       "        [99, 43, 25, 40, 64, 50, 18, 87, 42, 87],\n",
       "        [34, 55, 82, 41, 45, 87, 11, 21, 33,  9]]),\n",
       " array([[82, 44, 62, 23, 81, 51,  3, 56, 78, 12],\n",
       "        [66, 73, 21, 51, 51, 53,  1, 51,  1, 20],\n",
       "        [38, 42, 18, 83, 39, 11, 88,  7, 94, 12],\n",
       "        [57, 65, 39, 66, 58, 10, 48, 31, 93, 74],\n",
       "        [81, 57, 13, 27, 42, 82,  5, 22, 45,  0],\n",
       "        [73, 81, 85, 70, 31,  0,  8, 85, 59, 10]])]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参数给列表，根据列表中的索引进行切片\n",
    "np.split(a,indices_or_sections=[1,5,9]) # 指定从0-1,1-5,5,9,9-最后切分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "powered-performance",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 57, 56, 17, 21],\n",
       "        [62,  2, 14, 80, 15],\n",
       "        [54, 13, 53, 48, 11],\n",
       "        [83, 63, 73, 26, 77],\n",
       "        [83, 80, 78, 93, 54],\n",
       "        [22, 69, 54,  0, 58],\n",
       "        [62,  6,  8, 31,  8],\n",
       "        [99, 43, 25, 40, 64],\n",
       "        [34, 55, 82, 41, 45],\n",
       "        [82, 44, 62, 23, 81],\n",
       "        [66, 73, 21, 51, 51],\n",
       "        [38, 42, 18, 83, 39],\n",
       "        [57, 65, 39, 66, 58],\n",
       "        [81, 57, 13, 27, 42],\n",
       "        [73, 81, 85, 70, 31]]),\n",
       " array([[42, 35, 59, 96, 75],\n",
       "        [57, 80, 24, 50, 59],\n",
       "        [12, 54, 54, 47, 47],\n",
       "        [ 4, 15, 92,  3, 46],\n",
       "        [43, 75, 57,  9, 86],\n",
       "        [59, 81, 96, 62, 73],\n",
       "        [31,  4, 86, 99, 14],\n",
       "        [50, 18, 87, 42, 87],\n",
       "        [87, 11, 21, 33,  9],\n",
       "        [51,  3, 56, 78, 12],\n",
       "        [53,  1, 51,  1, 20],\n",
       "        [11, 88,  7, 94, 12],\n",
       "        [10, 48, 31, 93, 74],\n",
       "        [82,  5, 22, 45,  0],\n",
       "        [ 0,  8, 85, 59, 10]])]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a, indices_or_sections=2) # h表示水平切,列方向分割成2份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "transparent-turning",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 57, 56, 17, 21, 42, 35, 59, 96, 75],\n",
       "        [62,  2, 14, 80, 15, 57, 80, 24, 50, 59],\n",
       "        [54, 13, 53, 48, 11, 12, 54, 54, 47, 47]]),\n",
       " array([[83, 63, 73, 26, 77,  4, 15, 92,  3, 46],\n",
       "        [83, 80, 78, 93, 54, 43, 75, 57,  9, 86],\n",
       "        [22, 69, 54,  0, 58, 59, 81, 96, 62, 73],\n",
       "        [62,  6,  8, 31,  8, 31,  4, 86, 99, 14]]),\n",
       " array([[99, 43, 25, 40, 64, 50, 18, 87, 42, 87],\n",
       "        [34, 55, 82, 41, 45, 87, 11, 21, 33,  9],\n",
       "        [82, 44, 62, 23, 81, 51,  3, 56, 78, 12],\n",
       "        [66, 73, 21, 51, 51, 53,  1, 51,  1, 20]]),\n",
       " array([[38, 42, 18, 83, 39, 11, 88,  7, 94, 12],\n",
       "        [57, 65, 39, 66, 58, 10, 48, 31, 93, 74],\n",
       "        [81, 57, 13, 27, 42, 82,  5, 22, 45,  0],\n",
       "        [73, 81, 85, 70, 31,  0,  8, 85, 59, 10]])]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a, indices_or_sections=[3,7,11]) # v表示竖直切"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "therapeutic-conflict",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cognitive-photographer",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0,1,2,3]*3)\n",
    "arr1.sort() # 从小到大排序\n",
    "arr1 = arr1.reshape(4,3)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "nearby-frequency",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.array([1,2,3])\n",
    "display(arr1,arr2) # 形状不对应，但由于广播机制，依然可以计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "comfortable-rental",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行不够，广播行\n",
    "arr1 + arr2 # arr2 和arr1 中的每一行都进行相加： 广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "minute-elite",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([[1],[2],[3],[4]])\n",
    "display(arr1,arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "photographic-gambling",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列不够，广播列\n",
    "arr1 + arr3 # 广播，arr3和arr1中的每一列，进行相加。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lucky-middle",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 广播类似于复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "mediterranean-sellers",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "statistical-notification",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([0,1,2,3,4,5,6,7]).reshape(4,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "terminal-alexander",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b # b (4,2) 广播复制了3份"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "focused-pharmacology",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "terminal-wisconsin",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 元素级数字级别的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "infectious-phenomenon",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# abs、sqrt、square、exp、log、sin、cos、tan，maxinmum、\n",
    "# minimum、all、any、inner、clip、\n",
    "# round、trace、ceil、floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "healthy-carpet",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -2, -3, -5,  1,  5,  8,  9])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([-1,-2,-3,-5,1,5,8,9])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "genuine-annual",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 5, 1, 5, 8, 9])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "potential-growing",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 2.        , 2.82842712, 3.        , 4.        ,\n",
       "       5.        ])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([1,4,8,9,16,25])\n",
    "np.sqrt(arr1) # 开平⽅"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "thousand-brooklyn",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5773502691896257"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(arr1) # 平方\n",
    "np.exp(2) # e的多少次幂\n",
    "np.log(20.085536) # e的对数求解\n",
    "np.sin(np.pi/2) # 90度的sin\n",
    "np.tan(np.pi/6) # 30度的正切\n",
    "# 给两个数组，从中选取大的或小的\n",
    "np.maximum(a,c) # 从a，c中选取最大的值\n",
    "np.minimum(1,c) # 从a，c中选取最小的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "arctic-firmware",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-1, -3,  4,  8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.array([1,3,0])\n",
    "nd2 = np.array([-1,-3,4,8])\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "consolidated-bouquet",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.any() # 只要有一个true，返回true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "driving-divide",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.all() # 所有true，返回true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "superior-spokesman",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "fallen-scroll",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "b = np.array([1,2,3,4,5])\n",
    "np.inner(a,b)\n",
    "# inner返回两个数组的内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "agreed-bristol",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([57, 43,  8,  6, 81, 26, 55, 76, 12,  6, 76, 68, 26, 66, 32, 26, 62,\n",
       "       56, 38, 12, 39, 70, 61,  6, 65, 50,  5, 87, 67, 94])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,100,size = 30)\n",
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "invalid-sacramento",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([57, 43, 10, 10, 80, 26, 55, 76, 12, 10, 76, 68, 26, 66, 32, 26, 62,\n",
       "       56, 38, 12, 39, 70, 61, 10, 65, 50, 10, 80, 67, 80])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# clip 数据的裁剪，将小于10 的变成10，大于80 的变成80\n",
    "np.clip(nd1,10,80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "capable-configuration",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.41559821, -0.74472427,  1.29024271, -0.27890015, -0.32707484,\n",
       "       -0.13766172, -1.53196637, -1.2462582 ,  1.60428326, -0.24329735,\n",
       "        0.20537919,  1.29645482,  2.06725616, -0.94169319, -0.46240382,\n",
       "       -0.72407533, -0.61057874,  0.50251045,  1.10282042, -1.70074279])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.random.randn(20)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "violent-parallel",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.42, -0.74,  1.29, -0.28, -0.33, -0.14, -1.53, -1.25,  1.6 ,\n",
       "       -0.24,  0.21,  1.3 ,  2.07, -0.94, -0.46, -0.72, -0.61,  0.5 ,\n",
       "        1.1 , -1.7 ])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保留小数位\n",
    "nd2.round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "monetary-heather",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3.,  1., 14.])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 天花板，向上取整\n",
    "np.ceil(np.array([2.7,0.5,13.8])) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "talented-capture",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(2.99)# 向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "quantitative-stand",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 1, 5],\n",
       "       [2, 7, 9],\n",
       "       [0, 2, 8]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = (3,3))\n",
    "display(a)\n",
    "np.trace(a) # 计算对角线的和"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "straight-carrier",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### where 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "maritime-makeup",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "nd1 = np.array([1,3,5,7,9])\n",
    "nd2 = np.array([2,4,6,8,10])\n",
    "cond = np.array([True,False,False,True,True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "thick-comedy",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 6, 7, 9])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(cond,nd1,nd2) # 如果是true返回nd1的数据，false返回nd2的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "excessive-length",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 34, 68,  0,  2, 84, 49, 15, 87, 49, 76,  5, 45, 20, 44, 22, 35,\n",
       "       21, 58, 89, 84, 72, 70, 23,  2, 18,  9, 11,  0, 25, 11, 40, 76,  4,\n",
       "       29, 11, 47, 71, 22, 90, 17,  3, 23, 93, 86, 68, 44,  0, 17, 74])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-100, -100,   68, -100, -100,   84, -100, -100,   87, -100,   76,\n",
       "       -100, -100, -100, -100, -100, -100, -100,   58,   89,   84,   72,\n",
       "         70, -100, -100, -100, -100, -100, -100, -100, -100, -100,   76,\n",
       "       -100, -100, -100, -100,   71, -100,   90, -100, -100, -100,   93,\n",
       "         86,   68, -100, -100, -100,   74])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 50)\n",
    "display(a)# \n",
    "np.where(a > 50,a,-100) # 大于50，返回a的数据，不然返回-100\n",
    "# np.where(a > 50,a,a+20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "restricted-highland",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 92, 89, 69, 58, 22, 66, 27, 51, 87, 28, 91,  8, 10,  5, 75, 97,\n",
       "       51,  1, 99, 67, 54, 74, 86, 29, 64,  8, 90, 16, 51, 57, 32, 71, 27,\n",
       "       64, 52, 28, 79, 73, 65, 75, 93, 52, 58, 79, 60, 87, 31, 99, 97])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果分数在50-59之间，自动加10分\n",
    "a = np.random.randint(0,100,size = 50)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "atomic-simulation",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 92, 89, 69, 68, 22, 66, 27, 61, 87, 28, 91,  8, 10,  5, 75, 97,\n",
       "       61,  1, 99, 67, 64, 74, 86, 29, 64,  8, 90, 16, 61, 67, 32, 71, 27,\n",
       "       64, 62, 28, 79, 73, 65, 75, 93, 62, 68, 79, 60, 87, 31, 99, 97])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = (a >= 50) & (a < 60) # 与运算\n",
    "np.where(cond,a+10,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "suburban-maximum",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "given-mining",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([67,  4, 30, 77, 37, 72, 42, 82, 47,  7, 57, 83, 80, 90, 89,  7, 14,\n",
       "       94, 71, 12])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 20)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "equipped-toddler",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4,  7,  7, 12, 14, 30, 37, 42, 47, 57, 67, 71, 72, 77, 80, 82, 83,\n",
       "       89, 90, 94])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.sort(a) # 打印输出，原数组没有改变\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "checked-bradley",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "a.sort() # 没有输出，原数组进行了排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "solar-murray",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4,  7,  7, 12, 14, 30, 37, 42, 47, 57, 67, 71, 72, 77, 80, 82, 83,\n",
       "       89, 90, 94])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "missing-anaheim",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19], dtype=int64)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = a.argsort() # 返回排序的索引\n",
    "index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "governing-accent",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([94, 90, 89, 83, 82, 80, 77, 72, 71, 67, 57, 47, 42, 37, 30, 14, 12,\n",
       "        7,  7,  4])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据索引，花式索引\n",
    "a[index][::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "champion-stone",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 集合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "crazy-model",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 6, 7, 9, 1, 6, 4, 1, 7, 7, 0, 5, 5, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([5, 3, 4, 1, 4, 6, 1, 0, 4, 1, 5, 8, 1, 1, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = 15)\n",
    "b = np.random.randint(0,10,size = 15)\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "meaning-shanghai",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 4, 5, 6])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(a,b) # 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "retained-summit",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.union1d(a,b) # 并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "engaged-cyprus",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 9])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a,b) # 差集，a中有，b中没有"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "employed-evaluation",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数学和统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "compressed-timber",
   "metadata": {
    "hidden": true
   },
   "source": [
    "min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、argwhere、cov、corrcoef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "conventional-bridal",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[53, 68, 45, 58, 79],\n",
       "       [41, 69, 35, 32, 61],\n",
       "       [20, 34, 27, 71, 61]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size=(3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "eight-montana",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "cutting-complexity",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([53, 69, 45, 71, 79])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis = 0) # axis =0 行，1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "conscious-beauty",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([79, 69, 71])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "radio-affairs",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.266666666666666"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "acoustic-crest",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "53.0"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(a) # 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "satellite-hebrew",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "754"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "coupled-albania",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17.551701405340232"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std() # 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "subtle-independence",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "308.06222222222226"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var() # 方差，描述数据内部波动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "jewish-howard",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 53, 121, 166, 224, 303, 344, 413, 448, 480, 541, 561, 595, 622,\n",
       "       693, 754], dtype=int32)"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum() # 累加和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "arctic-burden",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([         53,        3604,      162180,     9406440,   743108760,\n",
       "         402688088,  2015674296,  1829123624, -1597586176,  1331491072,\n",
       "         860017664,  -824170496,  -777766912,   613124096, -1254135808],\n",
       "      dtype=int32)"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumprod()# 累乘和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "thousand-answer",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin() # 返回最小值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "discrete-protection",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax() # 返回最大值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "cloudy-evans",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[53, 68, 45, 58, 79],\n",
       "       [41, 69, 35, 32, 61],\n",
       "       [20, 34, 27, 71, 61]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "hybrid-province",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 1],\n",
       "       [0, 3],\n",
       "       [0, 4],\n",
       "       [1, 1],\n",
       "       [1, 4],\n",
       "       [2, 0],\n",
       "       [2, 2],\n",
       "       [2, 3],\n",
       "       [2, 4]], dtype=int64)"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere((a > 50) | (a < 30))# 返回符合条件的索引\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "balanced-survival",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53\n",
      "68\n",
      "58\n",
      "79\n",
      "69\n",
      "61\n",
      "20\n",
      "27\n",
      "71\n",
      "61\n"
     ]
    }
   ],
   "source": [
    "for i,j in index:\n",
    "    print(a[i,j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "organized-strain",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[175.3 , 173.05, 154.05],\n",
       "       [173.05, 270.8 ,  -8.7 ],\n",
       "       [154.05,  -8.7 , 493.3 ]])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cov 协方差（属性之间进行计算） 方差概念类似（数据累内部，属性内部计算）\n",
    "np.cov(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "transsexual-marsh",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.79424834,  0.52385994],\n",
       "       [ 0.79424834,  1.        , -0.02380342],\n",
       "       [ 0.52385994, -0.02380342,  1.        ]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(a) # 相关性系数，根据协方差计算（1~-1）\n",
    "# 0表示没有关系"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "verbal-efficiency",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "parallel-person",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 7, 0],\n",
       "       [5, 3, 7],\n",
       "       [3, 4, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 9, 1],\n",
       "       [7, 7, 6, 2],\n",
       "       [8, 3, 2, 9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = np.random.randint(0,10,size = (3,3))\n",
    "B = np.random.randint(0,10,size = (3,4))\n",
    "display(A,B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "prepared-reviewer",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 81,  89, 114,  22],\n",
       "       [ 97,  67,  77,  74],\n",
       "       [ 64,  52,  57,  38]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B) # 矩阵乘积A*B（点乘）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "ideal-waters",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 81,  89, 114,  22],\n",
       "       [ 97,  67,  77,  74],\n",
       "       [ 64,  52,  57,  38]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(A,B) # 模块提供的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "southeast-prime",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 81,  89, 114,  22],\n",
       "       [ 97,  67,  77,  74],\n",
       "       [ 64,  52,  57,  38]])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B # 表示矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "passing-music",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "C = np.random.randint(0,10,size = (4,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "gross-begin",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-132-36f3f9c6ed4d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mC\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0)"
     ]
    }
   ],
   "source": [
    "A.dot(C) # 形状不对应，无法进行计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "square-enclosure",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 60,  63, 119,  87, 112],\n",
       "       [ 69,  82, 139, 115, 140],\n",
       "       [ 51, 101, 135, 112, 151]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.dot(C) # 矩阵乘法不满足交换律"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "worst-russell",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 鸢尾花花萼属性各项指标"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unusual-limitation",
   "metadata": {
    "hidden": true
   },
   "source": [
    "案列：读取iris数据集中的花萼⻓度数据（已保存为csv格式）\n",
    "并对其进⾏排序、去重，并求出和、累积和、均值、标准差、⽅差、最⼩值、最⼤值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "electric-receiver",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n",
       "       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n",
       "       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n",
       "       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n",
       "       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n",
       "       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n",
       "       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n",
       "       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n",
       "       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n",
       "       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n",
       "       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n",
       "       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.loadtxt('./iris.csv') # 花萼的长度\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "prescribed-globe",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.4, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.7, 4.7, 4.8, 4.8,\n",
       "       4.8, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 4.9, 4.9, 5. , 5. , 5. , 5. ,\n",
       "       5. , 5. , 5. , 5. , 5. , 5. , 5.1, 5.1, 5.1, 5.1, 5.1, 5.1, 5.1,\n",
       "       5.1, 5.1, 5.2, 5.2, 5.2, 5.2, 5.3, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4,\n",
       "       5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.6, 5.6, 5.6, 5.6, 5.6, 5.6,\n",
       "       5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.8, 5.8, 5.8, 5.8, 5.8,\n",
       "       5.8, 5.8, 5.9, 5.9, 5.9, 6. , 6. , 6. , 6. , 6. , 6. , 6.1, 6.1,\n",
       "       6.1, 6.1, 6.1, 6.1, 6.2, 6.2, 6.2, 6.2, 6.3, 6.3, 6.3, 6.3, 6.3,\n",
       "       6.3, 6.3, 6.3, 6.3, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.5, 6.5,\n",
       "       6.5, 6.5, 6.5, 6.6, 6.6, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7,\n",
       "       6.8, 6.8, 6.8, 6.9, 6.9, 6.9, 6.9, 7. , 7.1, 7.2, 7.2, 7.2, 7.3,\n",
       "       7.4, 7.6, 7.7, 7.7, 7.7, 7.7, 7.9])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.sort(iris) #排序\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "preceding-mechanics",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5,\n",
       "       5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8,\n",
       "       6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.6, 7.7, 7.9])"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去重\n",
    "iris = np.unique(iris)\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "controversial-honey",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "210.39999999999998"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(iris)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "express-classroom",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.011428571428571"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(iris)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "interesting-finder",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  4.3,   8.7,  13.2,  17.8,  22.5,  27.3,  32.2,  37.2,  42.3,\n",
       "        47.5,  52.8,  58.2,  63.7,  69.3,  75. ,  80.8,  86.7,  92.7,\n",
       "        98.8, 105. , 111.3, 117.7, 124.2, 130.8, 137.5, 144.3, 151.2,\n",
       "       158.2, 165.3, 172.5, 179.8, 187.2, 194.8, 202.5, 210.4])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(iris) # 累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "graduate-score",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(iris) # 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "adaptive-elder",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0587265306122449"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(iris) # 方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "international-habitat",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0289443768310533"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(iris) # 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "blind-inside",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.3"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "polished-apparatus",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.9"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "adjusted-underwear",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.9"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(iris)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "233px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
